package com.atung.parking.config.auth;


import com.atung.parking.config.auth.entity.AuthUserEntity;
import com.atung.parking.entity.BusSuperUser;
import com.atung.parking.enums.AdminType;
import com.atung.parking.enums.SuperUserState;
import com.atung.parking.mapper.BusSuperUserMapper;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author Administrator
 */
@Service
public class AuthSecurityServiceImpl implements UserDetailsService {
    @Resource
    private BusSuperUserMapper busSuperUserMapper;
    @Override
    public AuthUserEntity loadUserByUsername(String userId) throws UsernameNotFoundException {

        BusSuperUser busSuperUser = busSuperUserMapper.selectById(userId);
        if (Objects.isNull(busSuperUser)) {
            return null;
        }
        //判断用户状态
        if(busSuperUser.getStatus().equals(SuperUserState.DISABLED.ordinal())){
            return null;
        }
        AuthUserEntity authUserEntity = new AuthUserEntity();
        authUserEntity.setUserId(busSuperUser.getId());
        authUserEntity.setUsername(busSuperUser.getUsername());
        authUserEntity.setName(authUserEntity.getName());
        authUserEntity.setPhone(authUserEntity.getPhone());
        authUserEntity.setPhoto("https://img1.baidu.com/it/u=366724989,267422008&fm=253&fmt=auto&app=120&f=JPEG?w=800&h=800");
        authUserEntity.setRoleType(AdminType.fromCode(busSuperUser.getType()));
        return authUserEntity;
    }


}
